package cn.atzxc.qy.service.impl;

import cn.atzxc.qy.domain.LoginUser;
import cn.atzxc.qy.domain.User;
import cn.atzxc.qy.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class userDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("username:手机号是："+username);
        //查询用户信息
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhoneNumber,username);

        User user = userMapper.selectOne(queryWrapper);
        System.out.println("user:---------"+user);
        //如果没有查询到用户就抛出异常
        if(Objects.isNull(user)){
            throw new RuntimeException("用户名或者密码错误");
        }
        LoginUser loginUser = new LoginUser(user);
        System.out.println("loginUser:"+loginUser);
        return new LoginUser(user);
    }
}
